<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>登录</title>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
</head>
<body>
<div style="width:100%;">
    <div style="margin: 200px auto;width:500px;height:400px;border:1px #E0E0E0 solid;">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>用户登录</legend>
        </fieldset>
        <div class="layui-form" style="margin:50px;"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" id="username" name="username" value="admin" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="password" name="password" value="123456" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="*" id="login_button">登录</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
            <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->
        </div>
    </div>
</div>
</body>
<script src="/plugins/jquery/jquery.min.js?v=1.12.4"></script>
<script src="/plugins/layui/layui.js"></script>
<script>
    $(function () {
        $("#login_button").bind("click", function () {
            login();
        })
        //绑定 回车键（enter）
        document.onkeyup = function(e){
            if(e.keyCode==13){
                login();
            }
        }
    });

    function login() {
        var username = $("#username").val();
        var password = $("#password").val();
        $.post("/login.json",
            {username: username, password: password},
            function (data) {
                if (200 == data.code) {
                    location.href = "/index.html"
                } else {
                    layui.use('layer', function(){
                        const layer = layui.layer;
                        layer.msg(data.msg);
                    });
                }
            }
        );
    }

</script>
<style>
    .layui-form-item {
    margin-bottom: 30px;
    clear: both;
    *zoom: 1;
}
</style>